package com.fizzkai.p2p.bussness.service.impl;

import com.fizzkai.p2p.bussness.domain.BidRequest;
import com.fizzkai.p2p.bussness.domain.BidRequestAuditHistory;
import com.fizzkai.p2p.bussness.mapper.BidRequestAuditHistoryMapper;
import com.fizzkai.p2p.bussness.service.IBidRequestAuditHistoryService;
import com.fizzkai.p2p.core.query.QueryObject;
import com.fizzkai.p2p.core.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service@Transactional
public class BidRequestAuditHistoryServiceImpl implements IBidRequestAuditHistoryService {
    @Autowired
    private BidRequestAuditHistoryMapper auditHistoryMapper;

    @Override
    public int save(BidRequestAuditHistory entity) {
        return auditHistoryMapper.insert(entity);
    }

    @Override
    public PageInfo<BidRequestAuditHistory> query(QueryObject qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        List<BidRequestAuditHistory> list = auditHistoryMapper.queryList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public List<BidRequestAuditHistory> queryListByRequestId(Long id) {
        return auditHistoryMapper.queryListByRequestId(id);
    }

    @Override
    public void createHistory(BidRequest bidRequest, int state, String remark, int auditType) {
        BidRequestAuditHistory history = new BidRequestAuditHistory();
        history.setBidRequestId(bidRequest.getId());    // 借款单 ID
        history.setApplier(bidRequest.getCreateUser());  // 借款人 冗余字段
        history.setApplyTime(bidRequest.getApplyTime());    // 借款申请时间 冗余字段
        history.setRemark(remark);  // 审核 提交的 备注
        history.setState(state);    // 审核 提交的 状态
        history.setAuditor(UserContext.getCurrentLoginInfo());  // 审核人
        history.setAuditTime(new Date());       // 审核时间
        history.setAuditType(auditType);    // 此次审核类型
        auditHistoryMapper.insert(history);
    }
}
